<?php

namespace App\Http\Requests;

use App\Util\ApiResult;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class MyInfoApiRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $this->needsJson();
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'tel' => ['bail','required','regex:/^1[3|4|5|7|8][0-9]{9}$/'],
            'username' =>  'regex:/^[\da-zA-Z\x80-\xff]{1,10}$/',
            'head' => 'mimes:jpeg,jpg,png,gif',

        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'tel.regex' => '请填写正确的联系电话',
            'username.regex' => '姓名只能是不超过10个字符的数字、英文、汉字',
            'head.mimes' => '上传的图片格式不正确',
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function formatErrors(Validator $validator)
    {
        return ApiResult::failure( $validator->errors()->first() )->getResult();
    }


}
